<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/myblogs/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblogs/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblogs/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblogs/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblogs/css/main.css">


<link rel="stylesheet" href="/myblogs/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/myblogs/lib/pace/pace-theme-minimal.min.css">
  <script src="/myblogs/lib/pace/pace.min.js"></script>


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('http://hackmgod.gitee.io').hostname,
    root: '/myblogs/',
    scheme: 'Gemini',
    version: '7.7.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="解释器模式（详解版）">
<meta property="og:type" content="article">
<meta property="og:title" content="【36】解释器模式（详解版）">
<meta property="og:url" content="http://hackmgod.gitee.io/2020/01/17/%E3%80%9036%E3%80%91%E8%A7%A3%E9%87%8A%E5%99%A8%E6%A8%A1%E5%BC%8F%EF%BC%88%E8%AF%A6%E8%A7%A3%E7%89%88%EF%BC%89/index.html">
<meta property="og:site_name" content="H.m博客">
<meta property="og:description" content="解释器模式（详解版）">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://c.biancheng.net/uploads/allimg/181119/3-1Q119150550114.gif">
<meta property="og:image" content="http://c.biancheng.net/uploads/allimg/181119/3-1Q119150626422.gif">
<meta property="og:image" content="http://c.biancheng.net/uploads/allimg/181119/3-1Q119150Q6401.gif">
<meta property="article:published_time" content="2020-01-17T05:59:17.000Z">
<meta property="article:modified_time" content="2020-01-17T10:09:33.442Z">
<meta property="article:author" content="H.m">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://c.biancheng.net/uploads/allimg/181119/3-1Q119150550114.gif">

<link rel="canonical" href="http://hackmgod.gitee.io/2020/01/17/%E3%80%9036%E3%80%91%E8%A7%A3%E9%87%8A%E5%99%A8%E6%A8%A1%E5%BC%8F%EF%BC%88%E8%AF%A6%E8%A7%A3%E7%89%88%EF%BC%89/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>【36】解释器模式（详解版） | H.m博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/myblogs/atom.xml" title="H.m博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/myblogs/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">H.m博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">www.hackm.cn</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/myblogs/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/myblogs/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/myblogs/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://hackmgod.gitee.io/2020/01/17/%E3%80%9036%E3%80%91%E8%A7%A3%E9%87%8A%E5%99%A8%E6%A8%A1%E5%BC%8F%EF%BC%88%E8%AF%A6%E8%A7%A3%E7%89%88%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="http://img.hao661.com/zt.hao661.com/uploads/allimg/141006/0644042451-0.jpg">
      <meta itemprop="name" content="H.m">
      <meta itemprop="description" content="欢迎您的到来！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="H.m博客">
    </span>
      <header class="post-header">
		
        <h1 class="post-title" itemprop="name headline">
          【36】解释器模式（详解版）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-01-17 13:59:17 / 修改时间：18:09:33" itemprop="dateCreated datePublished" datetime="2020-01-17T13:59:17+08:00">2020-01-17</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" itemprop="url" rel="index">
                    <span itemprop="name">设计模式</span>
                  </a>
                </span>
            </span>

          
            <div class="post-description">解释器模式（详解版）</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="【36】解释器模式（详解版）"><a href="#【36】解释器模式（详解版）" class="headerlink" title="【36】解释器模式（详解版）"></a>【36】解释器模式（详解版）</h2><p>在软件开发中，会遇到有些问题多次重复出现，而且有一定的相似性和规律性。如果将它们归纳成一种简单的语言，那么这些问题实例将是该语言的一些句子，这样就可以用“编译原理”中的解释器模式来实现了。</p>
<p>虽然使用解释器模式的实例不是很多，但对于满足以上特点，且对运行效率要求不是很高的应用实例，如果用解释器模式来实现，其效果是非常好的，本文将介绍其工作原理与使用方法。</p>
<h2 id="模式的定义与特点"><a href="#模式的定义与特点" class="headerlink" title="模式的定义与特点"></a>模式的定义与特点</h2><p>解释器（Interpreter）模式的定义：给分析对象定义一个语言，并定义该语言的文法表示，再设计一个解析器来解释语言中的句子。也就是说，用编译语言的方式来分析应用中的实例。这种模式实现了文法表达式处理的接口，该接口解释一个特定的上下文。</p>
<p>这里提到的文法和句子的概念同编译原理中的描述相同，“文法”指语言的语法规则，而“句子”是语言集中的元素。例如，汉语中的句子有很多，“我是中国人”是其中的一个句子，可以用一棵语法树来直观地描述语言中的句子。</p>
<p>解释器模式是一种类行为型模式，其主要优点如下。</p>
<ol>
<li>扩展性好。由于在解释器模式中使用类来表示语言的文法规则，因此可以通过继承等机制来改变或扩展文法。</li>
<li>容易实现。在语法树中的每个表达式节点类都是相似的，所以实现其文法较为容易。</li>
</ol>
<p>解释器模式的主要缺点如下。</p>
<ol>
<li>执行效率较低。解释器模式中通常使用大量的循环和递归调用，当要解释的句子较复杂时，其运行速度很慢，且代码的调试过程也比较麻烦。</li>
<li>会引起类膨胀。解释器模式中的每条规则至少需要定义一个类，当包含的文法规则很多时，类的个数将急剧增加，导致系统难以管理与维护。</li>
<li>可应用的场景比较少。在软件开发中，需要定义语言文法的应用实例非常少，所以这种模式很少被使用到。<h2 id="模式的结构与实现"><a href="#模式的结构与实现" class="headerlink" title="模式的结构与实现"></a>模式的结构与实现</h2>解释器模式常用于对简单语言的编译或分析实例中，为了掌握好它的结构与实现，必须先了解编译原理中的“文法、句子、语法树”等相关概念。<h6 id="1-文法"><a href="#1-文法" class="headerlink" title="1) 文法"></a>1) 文法</h6>文法是用于描述语言的语法结构的形式规则。没有规矩不成方圆，例如，有些人认为完美爱情的准则是“相互吸引、感情专一、任何一方都没有恋爱经历”，虽然最后一条准则较苛刻，但任何事情都要有规则，语言也一样，不管它是机器语言还是自然语言，都有它自己的文法规则。例如，中文中的“句子”的文法如下。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">〈句子〉::&#x3D;〈主语〉〈谓语〉〈宾语〉</span><br><span class="line">〈主语〉::&#x3D;〈代词〉|〈名词〉</span><br><span class="line">〈谓语〉::&#x3D;〈动词〉</span><br><span class="line">〈宾语〉::&#x3D;〈代词〉|〈名词〉</span><br><span class="line">〈代词〉你|我|他</span><br><span class="line">〈名词〉7大学生I筱霞I英语</span><br><span class="line">〈动词〉::&#x3D;是|学习</span><br></pre></td></tr></table></figure>


<p>==注：这里的符号“::=”表示“定义为”的意思，用“〈”和“〉”括住的是非终结符，没有括住的是终结符。==</p>
<h6 id="2-句子"><a href="#2-句子" class="headerlink" title="2) 句子"></a>2) 句子</h6><p>句子是语言的基本单位，是语言集中的一个元素，它由终结符构成，能由“文法”推导出。例如，上述文法可以推出“我是大学生”，所以它是句子。</p>
<h6 id="3-语法树"><a href="#3-语法树" class="headerlink" title="3) 语法树"></a>3) 语法树</h6><p>语法树是句子结构的一种树型表示，它代表了句子的推导结果，它有利于理解句子语法结构的层次。图 1 所示是“我是大学生”的语法树。</p>
<p><img src="http://c.biancheng.net/uploads/allimg/181119/3-1Q119150550114.gif" alt="句子“我是大学生”的语法树"><br>图1 句子“我是大学生”的语法树</p>
<p>有了以上基础知识，现在来介绍解释器模式的结构就简单了。解释器模式的结构与组合模式相似，不过其包含的组成元素比组合模式多，而且组合模式是对象结构型模式，而解释器模式是类行为型模式。</p>
<h4 id="1-模式的结构"><a href="#1-模式的结构" class="headerlink" title="1. 模式的结构"></a>1. 模式的结构</h4><p>解释器模式包含以下主要角色。</p>
<ol>
<li>抽象表达式（Abstract Expression）角色：定义解释器的接口，约定解释器的解释操作，主要包含解释方法 interpret()。</li>
<li>终结符表达式（Terminal    Expression）角色：是抽象表达式的子类，用来实现文法中与终结符相关的操作，文法中的每一个终结符都有一个具体终结表达式与之相对应。</li>
<li>非终结符表达式（Nonterminal Expression）角色：也是抽象表达式的子类，用来实现文法中与非终结符相关的操作，文法中的每条规则都对应于一个非终结符表达式。</li>
<li>环境（Context）角色：通常包含各个解释器需要的数据或是公共的功能，一般用来传递被所有解释器共享的数据，后面的解释器可以从这里获取这些值。</li>
<li>客户端（Client）：主要任务是将需要分析的句子或表达式转换成使用解释器对象描述的抽象语法树，然后调用解释器的解释方法，当然也可以通过环境角色间接访问解释器的解释方法。</li>
</ol>
<p>解释器模式的结构图如图 2 所示。</p>
<p><img src="http://c.biancheng.net/uploads/allimg/181119/3-1Q119150626422.gif" alt="解释器模式的结构图"><br>图2 解释器模式的结构图</p>
<h4 id="2-模式的实现"><a href="#2-模式的实现" class="headerlink" title="2. 模式的实现"></a>2. 模式的实现</h4><p>解释器模式实现的关键是定义文法规则、设计终结符类与非终结符类、画出结构图，必要时构建语法树，其代码结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;抽象表达式类</span><br><span class="line">interface AbstractExpression</span><br><span class="line">&#123;</span><br><span class="line">    public Object interpret(String info);    &#x2F;&#x2F;解释方法</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;终结符表达式类</span><br><span class="line">class TerminalExpression implements AbstractExpression</span><br><span class="line">&#123;</span><br><span class="line">    public Object interpret(String info)</span><br><span class="line">    &#123;</span><br><span class="line">        &#x2F;&#x2F;对终结符表达式的处理</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;非终结符表达式类</span><br><span class="line">class NonterminalExpression implements AbstractExpression</span><br><span class="line">&#123;</span><br><span class="line">    private AbstractExpression exp1;</span><br><span class="line">    private AbstractExpression exp2;</span><br><span class="line">    public Object interpret(String info)</span><br><span class="line">    &#123;</span><br><span class="line">        &#x2F;&#x2F;非对终结符表达式的处理</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;环境类</span><br><span class="line">class Context</span><br><span class="line">&#123;</span><br><span class="line">    private AbstractExpression exp;</span><br><span class="line">    public Context()</span><br><span class="line">    &#123;</span><br><span class="line">        &#x2F;&#x2F;数据初始化</span><br><span class="line">    &#125;</span><br><span class="line">    public void operation(String info)</span><br><span class="line">    &#123;</span><br><span class="line">        &#x2F;&#x2F;调用相关表达式类的解释方法</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="模式的应用实例"><a href="#模式的应用实例" class="headerlink" title="模式的应用实例"></a>模式的应用实例</h2><p>【例1】用解释器模式设计一个“韶粵通”公交车卡的读卡器程序。</p>
<p>说明：假如“韶粵通”公交车读卡器可以判断乘客的身份，如果是“韶关”或者“广州”的“老人” “妇女”“儿童”就可以免费乘车，其他人员乘车一次扣 2 元。</p>
<p>分析：本实例用“解释器模式”设计比较适合，首先设计其文法规则如下。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;expression&gt; ::&#x3D; &lt;city&gt;的&lt;person&gt;</span><br><span class="line">&lt;city&gt; ::&#x3D; 韶关|广州</span><br><span class="line">&lt;person&gt; ::&#x3D; 老人|妇女|儿童</span><br></pre></td></tr></table></figure>


<p>然后，根据文法规则按以下步骤设计公交车卡的读卡器程序的类图。</p>
<ul>
<li>定义一个抽象表达式（Expression）接口，它包含了解释方法 interpret(String    info)。</li>
<li>定义一个终结符表达式（Terminal Expression）类，它用集合（Set）类来保存满足条件的城市或人，并实现抽象表达式接口中的解释方法 interpret(Stringinfo)，用来判断被分析的字符串是否是集合中的终结符。</li>
<li>定义一个非终结符表达式（AndExpressicm）类，它也是抽象表达式的子类，它包含满足条件的城市的终结符表达式对象和满足条件的人员的终结符表达式对象，并实现 interpret(String info) 方法，用来判断被分析的字符串是否是满足条件的城市中的满足条件的人员。</li>
<li>最后，定义一个环境（Context）类，它包含解释器需要的数据，完成对终结符表达式的初始化，并定义一个方法 freeRide(String info) 调用表达式对象的解释方法来对被分析的字符串进行解释。其结构图如图 3 所示。</li>
</ul>
<p><img src="http://c.biancheng.net/uploads/allimg/181119/3-1Q119150Q6401.gif" alt="“韶粵通”公交车读卡器程序的结构图"><br>图3 “韶粵通”公交车读卡器程序的结构图</p>
<p>程序代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">package interpreterPattern;</span><br><span class="line">import java.util.*;</span><br><span class="line">&#x2F;*文法规则</span><br><span class="line">  &lt;expression&gt; ::&#x3D; &lt;city&gt;的&lt;person&gt;</span><br><span class="line">  &lt;city&gt; ::&#x3D; 韶关|广州</span><br><span class="line">  &lt;person&gt; ::&#x3D; 老人|妇女|儿童</span><br><span class="line">*&#x2F;</span><br><span class="line">public class InterpreterPatternDemo</span><br><span class="line">&#123;</span><br><span class="line">    public static void main(String[] args)</span><br><span class="line">    &#123;</span><br><span class="line">        Context bus&#x3D;new Context();</span><br><span class="line">        bus.freeRide(&quot;韶关的老人&quot;);</span><br><span class="line">        bus.freeRide(&quot;韶关的年轻人&quot;);</span><br><span class="line">        bus.freeRide(&quot;广州的妇女&quot;);</span><br><span class="line">        bus.freeRide(&quot;广州的儿童&quot;);</span><br><span class="line">        bus.freeRide(&quot;山东的儿童&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;抽象表达式类</span><br><span class="line">interface Expression</span><br><span class="line">&#123;</span><br><span class="line">    public boolean interpret(String info);</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;终结符表达式类</span><br><span class="line">class TerminalExpression implements Expression</span><br><span class="line">&#123;</span><br><span class="line">    private Set&lt;String&gt; set&#x3D; new HashSet&lt;String&gt;();</span><br><span class="line">    public TerminalExpression(String[] data)</span><br><span class="line">    &#123;</span><br><span class="line">        for(int i&#x3D;0;i&lt;data.length;i++)set.add(data[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    public boolean interpret(String info)</span><br><span class="line">    &#123;</span><br><span class="line">        if(set.contains(info))</span><br><span class="line">        &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">        return false;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;非终结符表达式类</span><br><span class="line">class AndExpression implements Expression</span><br><span class="line">&#123;</span><br><span class="line">    private Expression city&#x3D;null;    </span><br><span class="line">    private Expression person&#x3D;null;</span><br><span class="line">    public AndExpression(Expression city,Expression person)</span><br><span class="line">    &#123;</span><br><span class="line">        this.city&#x3D;city;</span><br><span class="line">        this.person&#x3D;person;</span><br><span class="line">    &#125;</span><br><span class="line">    public boolean interpret(String info)</span><br><span class="line">    &#123;</span><br><span class="line">        String s[]&#x3D;info.split(&quot;的&quot;);       </span><br><span class="line">        return city.interpret(s[0])&amp;&amp;person.interpret(s[1]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;环境类</span><br><span class="line">class Context</span><br><span class="line">&#123;</span><br><span class="line">    private String[] citys&#x3D;&#123;&quot;韶关&quot;,&quot;广州&quot;&#125;;</span><br><span class="line">    private String[] persons&#x3D;&#123;&quot;老人&quot;,&quot;妇女&quot;,&quot;儿童&quot;&#125;;</span><br><span class="line">    private Expression cityPerson;</span><br><span class="line">    public Context()</span><br><span class="line">    &#123;</span><br><span class="line">        Expression city&#x3D;new TerminalExpression(citys);</span><br><span class="line">        Expression person&#x3D;new TerminalExpression(persons);</span><br><span class="line">        cityPerson&#x3D;new AndExpression(city,person);</span><br><span class="line">    &#125;</span><br><span class="line">    public void freeRide(String info)</span><br><span class="line">    &#123;</span><br><span class="line">        boolean ok&#x3D;cityPerson.interpret(info);</span><br><span class="line">        if(ok) System.out.println(&quot;您是&quot;+info+&quot;，您本次乘车免费！&quot;);</span><br><span class="line">        else System.out.println(info+&quot;，您不是免费人员，本次乘车扣费2元！&quot;);   </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>程序运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">您是韶关的老人，您本次乘车免费！</span><br><span class="line">韶关的年轻人，您不是免费人员，本次乘车扣费2元！</span><br><span class="line">您是广州的妇女，您本次乘车免费！</span><br><span class="line">您是广州的儿童，您本次乘车免费！</span><br><span class="line">山东的儿童，您不是免费人员，本次乘车扣费2元！</span><br></pre></td></tr></table></figure>

<h2 id="模式的应用场景"><a href="#模式的应用场景" class="headerlink" title="模式的应用场景"></a>模式的应用场景</h2><p>前面介绍了解释器模式的结构与特点，下面分析它的应用场景。</p>
<ul>
<li>当语言的文法较为简单，且执行效率不是关键问题时。</li>
<li>当问题重复出现，且可以用一种简单的语言来进行表达时。</li>
<li>当一个语言需要解释执行，并且语言中的句子可以表示为一个抽象语法树的时候，如 XML 文档解释。</li>
</ul>
<p>==注意：解释器模式在实际的软件开发中使用比较少，因为它会引起效率、性能以及维护等问题。如果碰到对表达式的解释，在 Java 中可以用 Expression4J 或 Jep 等来设计。==</p>
<h2 id="模式的扩展"><a href="#模式的扩展" class="headerlink" title="模式的扩展"></a>模式的扩展</h2><p>在项目开发中，如果要对数据表达式进行分析与计算，无须再用解释器模式进行设计了，Java 提供了以下强大的数学公式解析器：Expression4J、MESP(Math Expression String Parser) 和 Jep 等，它们可以解释一些复杂的文法，功能强大，使用简单。</p>
<p>现在以 Jep 为例来介绍该工具包的使用方法。Jep 是 Java expression parser 的简称，即 Java 表达式分析器，它是一个用来转换和计算数学表达式的 Java 库。通过这个程序库，用户可以以字符串的形式输入一个任意的公式，然后快速地计算出其结果。而且 Jep 支持用户自定义变量、常量和函数，它包括许多常用的数学函数和常量。</p>
<p>使用前先下载 Jep 压缩包，解压后，将 jep-x.x.x.jar 文件移到选择的目录中，在 Eclipse 的“Java 构建路径”对话框的“库”选项卡中选择“添加外部 JAR(X)…”，将该 Jep 包添加项目中后即可使用其中的类库。</p>
<p>下面以计算存款利息为例来介绍。存款利息的计算公式是：本金x利率x时间=利息，其相关代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">package interpreterPattern;</span><br><span class="line">import com.singularsys.jep.*;</span><br><span class="line">public class JepDemo</span><br><span class="line">&#123;</span><br><span class="line">    public static void main(String[] args) throws JepException</span><br><span class="line">    &#123;</span><br><span class="line">        Jep jep&#x3D;new Jep();</span><br><span class="line">        &#x2F;&#x2F;定义要计算的数据表达式</span><br><span class="line">        String 存款利息&#x3D;&quot;本金*利率*时间&quot;;</span><br><span class="line">        &#x2F;&#x2F;给相关变量赋值</span><br><span class="line">        jep.addVariable(&quot;本金&quot;,10000);</span><br><span class="line">        jep.addVariable(&quot;利率&quot;,0.038);</span><br><span class="line">        jep.addVariable(&quot;时间&quot;,2);</span><br><span class="line">        jep.parse(存款利息);    &#x2F;&#x2F;解析表达式</span><br><span class="line">        Object accrual&#x3D;jep.evaluate();    &#x2F;&#x2F;计算</span><br><span class="line">        System.out.println(&quot;存款利息：&quot;+accrual);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>程序运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">存款利息：760.0</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/myblogs/2020/01/17/%E3%80%903%E3%80%91UML%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9B%BE%E5%8F%8A%E7%B1%BB%E5%9B%BE%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB/" rel="prev" title="【3】UML中的类图及类图之间的关系">
      <i class="fa fa-chevron-left"></i> 【3】UML中的类图及类图之间的关系
    </a></div>
      <div class="post-nav-item">
    <a href="/myblogs/2020/01/17/%E3%80%9039%E3%80%91%E7%BB%93%E6%9E%84%E5%9E%8B%E6%A8%A1%E5%BC%8F%E5%BA%94%E7%94%A8%E5%AE%9E%E9%AA%8C/" rel="next" title="【39】结构型模式应用实验">
      【39】结构型模式应用实验 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#【36】解释器模式（详解版）"><span class="nav-number">1.</span> <span class="nav-text">【36】解释器模式（详解版）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模式的定义与特点"><span class="nav-number">2.</span> <span class="nav-text">模式的定义与特点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模式的结构与实现"><span class="nav-number">3.</span> <span class="nav-text">模式的结构与实现</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#1-文法"><span class="nav-number">3.0.0.0.1.</span> <span class="nav-text">1) 文法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#2-句子"><span class="nav-number">3.0.0.0.2.</span> <span class="nav-text">2) 句子</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#3-语法树"><span class="nav-number">3.0.0.0.3.</span> <span class="nav-text">3) 语法树</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-模式的结构"><span class="nav-number">3.0.1.</span> <span class="nav-text">1. 模式的结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-模式的实现"><span class="nav-number">3.0.2.</span> <span class="nav-text">2. 模式的实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模式的应用实例"><span class="nav-number">4.</span> <span class="nav-text">模式的应用实例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模式的应用场景"><span class="nav-number">5.</span> <span class="nav-text">模式的应用场景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模式的扩展"><span class="nav-number">6.</span> <span class="nav-text">模式的扩展</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="H.m"
      src="http://img.hao661.com/zt.hao661.com/uploads/allimg/141006/0644042451-0.jpg">
  <p class="site-author-name" itemprop="name">H.m</p>
  <div class="site-description" itemprop="description">欢迎您的到来！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblogs/archives/">
        
          <span class="site-state-item-count">42</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblogs/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/hackmgod" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;hackmgod" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/myblogs/hackmgod@foxmail.com" title="E-Mail → hackmgod@foxmail.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="/myblogs/975643993@qq.com" title="QQ → 975643993@qq.com"><i class="fa fa-fw fa-qq"></i>QQ</a>
      </span>
      <span class="links-of-author-item">
        <a href="/myblogs/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.baidu.com/" title="https:&#x2F;&#x2F;www.baidu.com&#x2F;" rel="noopener" target="_blank">百度</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.iqiyi.com/" title="https:&#x2F;&#x2F;www.iqiyi.com&#x2F;" rel="noopener" target="_blank">爱奇艺</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        <!-- 用下面的符号注释，注释代码用下面括号括起来 -->
<!-- -->

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">H.m</span>
</div>


<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.7.0
  </div> -->

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>





        








      </div>
    </footer>
  </div>

  
  <script src="/myblogs/lib/anime.min.js"></script>
  <script src="/myblogs/lib/velocity/velocity.min.js"></script>
  <script src="/myblogs/lib/velocity/velocity.ui.min.js"></script>

<script src="/myblogs/js/utils.js"></script>

<script src="/myblogs/js/motion.js"></script>


<script src="/myblogs/js/schemes/pisces.js"></script>


<script src="/myblogs/js/next-boot.js"></script>




  




  
<script src="/myblogs/js/local-search.js"></script>













  

  

</body>
</html>


<a href="https://github.com/hackmgod" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>


<!-- 动态背景 -->
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>